package com.liveinstars.common;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by guoyanjiang on 2018/10/22.
 */
public class DateUtil {

    public static String yyyyMMdd = "yyyyMMdd";

    /**
     * 获取当天剩余时间 秒
     *
     * @return
     */
    public static int getTodayResideSec() {
        LocalDateTime midnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        int todayResideSec = (int) LocalDateTime.now().until(midnight, ChronoUnit.SECONDS);
        return todayResideSec;
    }

    //获取半年前时间
    public static Date getHalfYearDate() {
        Calendar c = Calendar.getInstance();

        c.setTime(new Date());
        c.add(Calendar.MONTH, -6);

        return c.getTime();
    }

    //获取第几天前0点时间戳
    public static long getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);

        ParsePosition pos = new ParsePosition(0);
        Date strtodate = format.parse(result, pos);
        return strtodate.getTime() / 1000;
    }


    public static Date getStartDayOfWeek(LocalDate localDate) {
        LocalDate newDate = localDate.with(DayOfWeek.MONDAY);
        return Date.from(newDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    public static Date getEndDayOfWeek(LocalDate localDate) {
        LocalDate newDate = localDate.with(DayOfWeek.SUNDAY);
        return Date.from(newDate.atStartOfDay(ZoneId.systemDefault()).plusDays(1L).minusNanos(1L).toInstant());
    }

    public static String timestampToDate(String format, long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(timestamp);
    }

    /**
     * @return long
     * @Title 获取传入时间的最近8AM的时间戳
     * @Author ting
     * @Description TODO
     * @Date 2019-03-14 14:43:49
     * @Param [timeStampInMillis]
     **/
    public static long getNearest1DayOn8AmTimestampByTimestamp(long timestampInMillis) {
        Calendar calendar = Calendar.getInstance();//获取当前时间
        calendar.setTimeInMillis(timestampInMillis);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if (hour <= 8) {
            calendar.set(Calendar.DAY_OF_MONTH, -1);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * @return long
     * @Title 获取传入时间的最近00，5,10,15,20,25,30,35,40,45,50,55分的时间戳
     * @Author ting
     * @Description TODO
     * @Date 2019-03-14 14:45:03
     * @Param [timeStampInMillis]
     **/
    public static long getNearest5MinutesTimestampByTimestamp(long timestampInMillis) {
        Calendar calendar = Calendar.getInstance();//获取当前时间
        calendar.setTimeInMillis(timestampInMillis);
        calendar.set(Calendar.MINUTE, -5);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int minute = calendar.get(Calendar.MINUTE);// 得到分钟
        int count = 0;
        if (minute != 0
                && minute != 5
                && minute != 10
                && minute != 15
                && minute != 20
                && minute != 25
                && minute != 30
                && minute != 35
                && minute != 40
                && minute != 45
                && minute != 50
                && minute != 55) { //时间不为00，5,10,15,20,25,30,35,40,45,50,55的情况
            for (int i = minute; i >= 0; i--) { //判断距离最近的00，5,10,15,20,25,30,35,40,45,50,55的分钟数
                if (i % 5 == 0) {
                    break;
                } else {
                    count++;
                }
            }
        }
        calendar.add(Calendar.MINUTE, -count); //获取最近的分钟
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static long getDate8AMTimestamp(long timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp);
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.add(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取某周的开始日期
     *
     * @param offset 0本周，1下周，-1上周，依次类推
     * @return
     */
    public static long weekStart(int offset) {
        LocalDate localDate = LocalDate.now().plusWeeks(offset);
        return localDate.with(DayOfWeek.MONDAY).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000;
    }

    public static long weekEnd(int offset) {
        LocalDate localDate = LocalDate.now().plusWeeks(offset);
        return localDate.with(DayOfWeek.SUNDAY).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000;
    }


    /**
     * 获取某月的开始日期
     *
     * @param offset 0本月，1下个月，-1上个月，依次类推
     * @return
     */
    public static long monthStart(int offset) {
        return LocalDate.now().plusMonths(offset).with(TemporalAdjusters.firstDayOfMonth())
                .atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000;
    }

    public static long monthEnd(int offset) {
        return LocalDate.now().plusMonths(offset).with(TemporalAdjusters.lastDayOfMonth())
                .atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000;
    }

    //比较是否是同一天
    public static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                .get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }

    /**
     * @return long 毫秒
     * @Title TODO
     * @Author ting
     * @Description 获取传入日期时间戳的当日开始时间戳
     * @Date 2019-03-25 14:46:15
     * @Param [dateTimeInMillis]
     **/
    public static long getDateStartTime(long dateTimeInMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(dateTimeInMillis);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * @return long 毫秒
     * @Title TODO
     * @Author ting
     * @Description 获取传入日期时间戳的当日结束时间戳
     * @Date 2019-03-25 14:46:29
     * @Param [dateTimeInMillis]
     **/
    public static long getDateEndTime(long dateTimeInMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(dateTimeInMillis);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    /**
     * 获得昨日该时刻的时间戳
     *
     * @param time
     * @return
     */
    public static Integer getYesterdayTime(Integer time) {
        return time - 86400;
    }

    /**
     * 获得当前时间的时间戳
     *
     * @return
     */
    public static Integer getNowTime() {
        Long i = System.currentTimeMillis() / 1000;
        return i.intValue();
    }

    //根据入参时间戳获取年月日时间戳
    public static long getYMDtime(long date) {
        LocalDate localDate = Instant.ofEpochMilli(date * 1000).atZone(ZoneId.systemDefault()).toLocalDate();
        return localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000;
    }

}
